package com.teapos.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

@SuppressWarnings({"rawtypes", "unchecked"})
public class Tool {
	
	public static int toInt(Object obj) {
		return obj==null||StringUtils.isBlank(obj+"")?0:Integer.parseInt(obj+"");
	}
	
	public static String toString(Object obj) {
		return obj==null?"":obj+"";
	}
	
	public static long toLong(Object obj) {
		return obj==null?0L:Long.parseLong(obj+"");
	}
	
	public static double toDouble(Object obj) {
		return obj==null||StringUtils.isBlank(obj+"")?0d:Double.parseDouble(obj+"");
	}
	
	public static BigDecimal toBigDecimal(Object obj) {
		return obj==null||StringUtils.isBlank(obj+"")?new BigDecimal("0.00"):new BigDecimal(obj+"");
	}
	
	public static String getCookieValue(HttpServletRequest req, String key) {
		return getCookieValue(req, key, true);
	}
	
	public static String getCookieValue(HttpServletRequest req, String key, boolean flag) {
		for(Cookie cookie:req.getCookies()) {
			if(cookie.getName().equals(key)) {
				String value = cookie.getValue();
				System.err.println(value);
				try {
					value = flag?URLDecoder.decode(value, "utf-8"):value;
					System.err.println(value);
				} catch (UnsupportedEncodingException e) {
					throw new RuntimeException(e);
				}
				return value;
			}
		}
		return null;
	}
	
	public static Cookie getCookie(HttpServletRequest req, String key) {
		for(Cookie cookie:req.getCookies()) {
			if(cookie.getName().equals(key)) return cookie;
		}
		return null;
	}
	
	public static void printCookie(HttpServletRequest req) {
		for(Cookie cookie:req.getCookies()) {
			System.err.println(cookie.getName()+":"+cookie.getValue());
		}
	}
	
	public static String formatMoney(long money) {
		String pattern = "#,###.00";
		NumberFormat nf = new DecimalFormat(pattern);
		return nf.format(money);
	}
	
	public static String formatDate(Date date, String pattern) {
		return new SimpleDateFormat(pattern).format(date);
	}
	
	public static String getRandomNo(int len) {
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<len;i++) {
			sb.append(new SecureRandom().nextInt(9));
		}
		return sb.toString();
	}
	
	/*public static void main(String[] args) {
		formatMoney(0L);
	}*/

	public static Map initPage(Map map) {
		int pageNo = initPageNo(map.get("pageNo"));
		int start = (pageNo-1)*10;
		Maps.put(map, "start", start, "pageSize", 10);
		return map;
	}
	
	public static int initPageNo(Object org) {
		int now = toInt(org);
		return now==0?1:now;
	}
	
	public static Map dealParam(Map paramMap) {
		Iterator iter = paramMap.entrySet().iterator();
		while(iter.hasNext()) {
			Map.Entry param = (Map.Entry)iter.next();
			Object val = param.getValue();
			/*if(param.getKey().equals("has_save")) {
				System.err.println(val);
				System.err.println(val==null);
				System.err.println(val.equals(""));
			}*/
			if(val instanceof String) {
				param.setValue((val+"").replaceAll("%", ""));
			}
		}
		return paramMap;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(URLDecoder.decode("", "utf-8"));
	}

	public static Date toDate(Object obj) {
		return obj==null?null:(Date)obj;
	}

	public static boolean toBoolean(Object obj) {
		return obj==null?false:(Boolean)obj;
	}
	
}
